/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updatetree;

import java.io.*;
import java.text.ParseException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.*;



/**
 *
 * @author mani
 */
class XMLClass {

   
    private Document read;

    private String filename;
    private final XmlConfigureation xml;
 

    XMLClass( String udir,String file) throws DocumentException {
        filename =udir + File.separatorChar+ "xml" +  File.separatorChar + file;

        xml = new XmlConfigureation(udir,"configuration.xml");
        SAXReader sr2 = new SAXReader();
        read = sr2.read(filename);
       

    }

    void Save() throws IOException {

        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter write = new XMLWriter(new FileWriter(filename), format);
        write.write(read);
        write.close();
    }

    Element getRootElement() {
        return read.getRootElement();
    }

    boolean isUpdateDate(Element next) throws ParseException, Exception {

        String value = next.attribute("date").getValue();
        S2D d2 = new S2D(value);

        int compareTo = d2.getDate().compareTo(d2.getToday());
        if (Math.abs(compareTo) > xml.getConfigurationValueInt("date")) {
            return true;
        }

        return false;
    }

    void saveUpdateDateToday(Element next) throws ParseException {
        S2D tmp = new S2D("2000-01-01");
        next.addAttribute("date", tmp.getTodayChinese());
    }
}
